package com.cumt.framework.rest.view;

import com.cumt.framework.Constants;

import java.io.Serializable;

/**
 * Created by caicf on 2016/12/27.
 */
public class JsonViewResponse implements Serializable {

    private Object status;
    private Object message;
    private Object content;

    public Object getStatus() {
        return status;
    }

    public void setStatus(Object status) {
        this.status = status;
    }

    public Object getMessage() {
        return message;
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    /**
     * 因在table_template.js文件中，请求deleteByIds方法时，
     * 如果删除成功，需要对status和content同时校验
     * 固，此处增加success方法，用来固定后台删除成功后的调用，
     * 避免前后台数据格式不同意导致前台提示删除失败假象
     */
    public JsonViewResponse success() {
        this.setContent("true");
        this.setStatus(Constants.jsonView.STATUS_SUCCESS);
        return this;
    }

    public JsonViewResponse successPack(Object result) {
        this.setMessage("");
        this.setContent(result);
        this.setStatus(Constants.jsonView.STATUS_SUCCESS);
        return this;
    }

    public JsonViewResponse successPack(Object result, String msg) {
        this.setContent(result);
        this.setMessage(msg);
        this.setStatus(Constants.jsonView.STATUS_SUCCESS);
        return this;
    }

    public JsonViewResponse failPack(Exception e) {
        String message = e.getMessage();
        int index = message.indexOf(":");
        setMessage(index == -1 ? message : message.substring(index + 1));
        setContent("");
        setStatus(Constants.jsonView.STATUS_FAIL);
        return this;
    }

    public JsonViewResponse failPack(String errMsg) {
        setMessage(errMsg);
        setContent("");
        setStatus(Constants.jsonView.STATUS_FAIL);
        return this;
    }

    public JsonViewResponse failPack(String result, String errMsg) {
        setMessage(errMsg);
        setContent(result);
        setStatus(Constants.jsonView.STATUS_FAIL);
        return this;
    }

    public JsonViewResponse failPackMessage(String errMsg, String content) {
        setMessage(errMsg);
        setContent(content);
        setStatus(Constants.jsonView.STATUS_FAIL);
        return this;
    }


}
